package com.ecube.maintenance.components.fragment;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ecube.maintenance.R;
import com.ecube.maintenance.biz.apis.impl.OrderAction;
import com.ecube.maintenance.biz.commons.Constants;
import com.ecube.maintenance.biz.commons.ICCallBack;
import com.ecube.maintenance.biz.commons.ICTException;
import com.ecube.maintenance.components.activity.MainActivity;
import com.ecube.maintenance.components.widget.LoadingDialog;
import com.ecube.maintenance.pojos.OrderInfo;
import com.ecube.maintenance.pojos.UserInfo;
import com.ecube.maintenance.utils.Bog;

import java.util.List;

/**
 * #1
 * Created by wcx on 14-9-7.
 * 历史订单展示页面
 */

public class OrderedListFragment extends Fragment {
    ListView orderList;
    UserInfo userInfo;
    List<OrderInfo> orderInfoList;
    LoadingDialog loadingDialog;
    OrderedListAdapter mOrderListAdapter;

    public OrderedListFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static OrderedListFragment newInstance() {
        OrderedListFragment fragment = new OrderedListFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_order, container, false);
        orderList = (ListView) rootView.findViewById(R.id.ordered_list);
        userInfo = UserInfo.getInstance();
        loadingDialog = new LoadingDialog(getActivity());
        loadingDialog.setTipText(getString(R.string.message_loading_order_list));
        mOrderListAdapter = new OrderedListAdapter(getActivity());
        orderList.setAdapter(mOrderListAdapter);
        fetchOrderList();
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(1);
    }

    private ActionBar getActionBar() {
        return getActivity().getActionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        configureActionBar();
    }

    private void configureActionBar() {
        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        View v = actionBar.getCustomView();
        if (v != null) {
            v.setVisibility(View.GONE);
        }
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("订单列表");
    }

    void fetchOrderList() {
        loadingDialog.show();
        OrderAction.fetchOrderList(userInfo.getSid(), Constants.ORDER_DATA_TYPE_LAST,
                new ICCallBack<List<OrderInfo>>() {
                    @Override
                    public void done(List<OrderInfo> result, ICTException exception) {
                        if (exception != null) {
                            Bog.toast(exception.getMessage());
                        } else {
                            orderInfoList = result;
                            mOrderListAdapter.refresh();
                        }
                        loadingDialog.getDialog().dismissDialogger();
                    }
                });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        configureActionBar();
        fetchOrderList();
    }

    /**
     * 订单适配器
     */
    private class OrderedListAdapter extends BaseAdapter {
        LayoutInflater inflater;
        Context mContext;

        public OrderedListAdapter(Context context) {
            mContext = context;
            this.inflater = LayoutInflater.from(context);
        }

        public void refresh() {
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return orderInfoList == null ? 0 : orderInfoList.size();
        }

        @Override
        public Object getItem(int position) {
            return orderInfoList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final OrderInfo orderInfo = orderInfoList.get(position);
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = this.inflater.inflate(R.layout.item_list_order, null);
                holder.orderTime = (TextView) convertView.findViewById(R.id.item_list_order_create_time);
                holder.price = (TextView) convertView.findViewById(R.id.item_list_order_price);
                holder.productName = (TextView) convertView.findViewById(R.id.item_list_order_title);
                holder.spName = (TextView) convertView.findViewById(R.id.item_list_order_dealer);
                holder.status = (TextView) convertView.findViewById(R.id.item_list_order_status);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.orderTime.setText(orderInfo.getCreateTime());
            holder.spName.setText(orderInfo.getSpName());
            String status = orderInfo.getOrderStatus();
            holder.status.setText(status);
            if ("已预订".equals(status)
                    || "已确认".equals(status)
                    || "已到店".equals(status)) {
                holder.status.setTextColor(getResources().getColor(R.color.red));
            } else if ("已取消".equals(status)) {
                holder.status.setTextColor(getResources().getColor(R.color.Gray));
            } else {
                holder.status.setTextColor(getResources().getColor(R.color.Green));
            }
            holder.productName.setText(orderInfo.getProductName());
            holder.price.setText("￥" + orderInfo.getProductPrice());
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getFragmentManager().beginTransaction().hide(OrderedListFragment.this)
                            .add(R.id.container, OrderDetailFragment.newInstance(orderInfo))
                            .addToBackStack(null).commit();
                }
            });
            return convertView;
        }

        public class ViewHolder {
            TextView productName, spName, orderTime, price, status;
        }
    }


}
